package com.zhongwang.mms.module.basic.controller;

import com.zhongwang.mms.base.BaseController;
import com.zhongwang.mms.common.JsonResult;
import com.zhongwang.mms.gen.entity.MMaterials;
import com.zhongwang.mms.module.basic.model.MMaterialsModel;
import com.zhongwang.mms.module.basic.service.MMaterialDictionaryService;
import com.zhongwang.mms.module.basic.service.MMaterialsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Description: 添加原料基础数据 控制层
 * @Author: China.wsg
 * @Date: 2020/03/16 16:21
 */
@Controller
@RequestMapping("/basic/materials")
public class MMaterialsController extends BaseController {

    @Autowired
    private MMaterialsService mMaterialsService;
    @Autowired
    private MMaterialDictionaryService mMaterialDictionaryService;

    /**
     * 添加原料数据字典 页面
     *
     * @return 页面路径
     */
    @GetMapping("/page")
    @RequiresPermissions({"basic:matinfo:view"})
    public String page(Model model) {
        model.addAttribute("materialStock", mMaterialsService.findMaterialStockByOrderByStockNameAsc(""));
        return "basic/materials/list";
    }

    /**
     * 添加页面
     */
    @GetMapping("/create")
    @RequiresPermissions({"basic:matinfo:edit"})
    public String create(Model model) {
        model.addAttribute("materialStock", mMaterialsService.findMaterialStockByOrderByStockNameAsc(""));
        return "basic/materials/create";
    }

    /**
     * 根据分类名称获取类型、材质、规格下拉选内容
     *
     * @param kindName 分类名称
     * @return
     */
    @ResponseBody
    @GetMapping("/materialDictionary")
    public JsonResult getMaterialDictionaryByKindName(String kindName) {

        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(mMaterialDictionaryService.findMaterialDictionaryByKindName(kindName));
        jsonResult.setType(JsonResult.ResultType.SUCCESS);
        return jsonResult;
    }

    /**
     * 根据条件获取重量公式
     *
     * @param matTypeId 类型Id
     * @return
     */
    @ResponseBody
    @GetMapping("/getFormulaType")
    public JsonResult getFormulaType(Integer matTypeId) {

        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(mMaterialsService.getFormulaType(matTypeId));
        jsonResult.setType(JsonResult.ResultType.SUCCESS);
        return jsonResult;
    }

    /**
     * 保存原料基础信息
     *
     * @param materials 保存信息
     * @return json 数据
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions({"basic:matinfo:edit"})
    public JsonResult save(MMaterials materials) {
        JsonResult jsonResult = new JsonResult();
        String msg = mMaterialsService.saveMaterials(materials);
        if (msg.equals("")) {
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
        }
        jsonResult.setMsg(msg);
        return jsonResult;
    }

    /**
     * 原料基础列表
     *
     * @param materialsModel 查询条件
     * @return list
     */
    @ResponseBody
    @PostMapping("/list")
    @RequiresPermissions({"basic:matinfo:view"})
    public JsonResult list(MMaterialsModel materialsModel) {
        startPage();
        List<MMaterialsModel> list = mMaterialsService.findMaterialsModelByQuery(materialsModel);
        return jsonResult(list);
    }

    /**
     * 删除原料基础字典
     *
     * @param materials 被删除数据
     * @return json 数据
     */
    @ResponseBody
    @PostMapping("/del")
    @RequiresPermissions({"basic:matinfo:edit"})
    public JsonResult del(MMaterials materials) {
        JsonResult jsonResult = new JsonResult();
        String msg = mMaterialsService.delMMaterials(materials);
        if (msg.equals("")) {
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
        }
        jsonResult.setMsg(msg);
        return jsonResult;
    }

    /**
     * 修改原料基础数据
     *
     * @param materials 保存信息
     * @return json 数据
     */
    @ResponseBody
    @PostMapping("/update")
    @RequiresPermissions({"basic:matinfo:edit"})
    public JsonResult update(MMaterials materials) {
        JsonResult jsonResult = new JsonResult();
        String msg = mMaterialsService.updateMaterials(materials);
        if (msg.equals("")) {
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
        }
        jsonResult.setMsg(msg);
        return jsonResult;
    }

    /**
     * 导入物料
     *
     * @return
     */
    @GetMapping("/import")
    @RequiresPermissions({"basic:matinfo:edit"})
    public String importMaterial() {
        return "basic/materials/import";
    }

    /**
     * 导入物料
     *
     * @param file 导入文件
     * @return JsonResult
     */
    @ResponseBody
    @PostMapping("/importFile")
    @RequiresPermissions({"matinv:matcheckresult:edit"})
    public JsonResult importFile(@RequestParam("file") MultipartFile file, HttpServletResponse response) {
        String msg = mMaterialsService.importFile(file);
        JsonResult jsonResult;
        if ("导入失败".equals(msg)) {
            jsonResult = error();
        } else {
            jsonResult = success();
        }
        jsonResult.setMsg(msg);
        return jsonResult;
    }

    /**
     * 判断文件是否存在
     *
     * @param url 文件路径
     * @return
     */
    @ResponseBody
    @PostMapping("/exist")
    public JsonResult exist(String url) {
        return jsonResult(mMaterialsService.exist(url));
    }

}
